package com.wiigeetest;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.ToggleButton;
import android.hardware.Sensor;
import android.hardware.SensorManager;

import org.wiigee.control.AndroidWiigee;
import org.wiigee.event.GestureEvent;
import org.wiigee.event.GestureListener;

public class WiigeeTest extends Activity
{
	public static final int TRAIN_BUTTON = 10;
	public static final int CLOSE_GESTURE_BUTTON = 11;
	public static final int RECOGNITION_BUTTON = 12;
	AndroidWiigee andgee;
	SensorManager sm;

	RelativeLayout rlMain;
	TextView txtTitle;
	Button btnTrain;
	ToggleButton btnRecognize;
	TextView txtGesture;
	Spinner spnGestures;
	ArrayAdapter<CharSequence> aa;
	Button btnDelete;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        andgee = new AndroidWiigee(getApplicationContext());
        andgee.setTrainButton(TRAIN_BUTTON);
        andgee.setCloseGestureButton(CLOSE_GESTURE_BUTTON);
        andgee.setRecognitionButton(RECOGNITION_BUTTON);
        
        andgee.addGestureListener(new GestureListener() {
			
			@Override
			public void gestureReceived(GestureEvent event) {
				if (event.getProbability() < 0.1)
					txtGesture.setText("No gesture recognized!");
				else
					txtGesture.setText("Gesture " + spnGestures.getItemAtPosition(event.getId()).toString() + " recognized with " + event.getProbability() * 100 + "% probability.");
			}
		});
        
        sm = (SensorManager)getSystemService(SENSOR_SERVICE);
        sm.registerListener(andgee.getDevice(), sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_FASTEST);
        try {
			andgee.getDevice().setAccelerationEnabled(true);
		} catch (IOException e) {
			e.printStackTrace();
		}
        
		rlMain = (RelativeLayout)findViewById(R.id.rlMain);
		txtTitle = (TextView)findViewById(R.id.txtTitle);
		
		btnTrain = (Button)findViewById(R.id.btnTrain);
		btnTrain.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				startActivity(new Intent(WiigeeTest.this, TrainingScreen.class));
			}
		});
		
		btnRecognize = (ToggleButton)findViewById(R.id.btnRecognize);
		btnRecognize.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if (btnRecognize.isChecked())
				{
					btnDelete.setEnabled(false);
					andgee.getDevice().fireButtonPressedEvent(RECOGNITION_BUTTON);
				}
				else
				{
					btnDelete.setEnabled(true);
					andgee.getDevice().fireButtonReleasedEvent(RECOGNITION_BUTTON);
				}
			}
		});
		
		txtGesture = (TextView)findViewById(R.id.txtGesture);
		
		new File(getFilesDir().toString()).mkdirs();
		spnGestures = (Spinner)findViewById(R.id.spnGestures);
		aa = new ArrayAdapter<CharSequence>(this, android.R.layout.simple_spinner_item);
		aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spnGestures.setAdapter(aa);
		updateList();
		
		btnDelete = (Button)findViewById(R.id.btnDelete);
		btnDelete.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				File f = null;
				if (!aa.isEmpty())
					f = new File(getFilesDir().toString() + "/" + spnGestures.getSelectedItem().toString());
				if (f != null && f.exists()) f.delete();
				updateList();
			}
		});
    }
    
    public void onResume()
    {
    	super.onResume();
    	andgee = new AndroidWiigee(getApplicationContext());
        andgee.setTrainButton(TRAIN_BUTTON);
        andgee.setCloseGestureButton(CLOSE_GESTURE_BUTTON);
        andgee.setRecognitionButton(RECOGNITION_BUTTON);
        
        andgee.addGestureListener(new GestureListener() {
			
			@Override
			public void gestureReceived(GestureEvent event) {
				if (event.getProbability() < 0.1)
					txtGesture.setText("No gesture recognized!");
				else
					txtGesture.setText("Gesture " + spnGestures.getItemAtPosition(event.getId()).toString() + " recognized with " + event.getProbability() * 100 + "% probability.");
			}
		});
        sm = (SensorManager)getSystemService(SENSOR_SERVICE);
        sm.registerListener(andgee.getDevice(), sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_FASTEST);
        try {
			andgee.getDevice().setAccelerationEnabled(true);
		} catch (IOException e) {
			e.printStackTrace();
		}
    	updateList();
    }
    
    private void updateList()
    {
    	File f = new File(getFilesDir().toString());
    	aa.clear();
		for (String fn : f.list())
		{
			try
			{
			FileInputStream fi = openFileInput(fn);
			int ch;
			String s = "";
			while( (ch = fi.read()) != -1)
		        s += (char)ch;
			
			andgee.getDevice().loadGesture(s);
			aa.add(fn);
			}
			catch (IOException e)
			{
				System.out.println("Read error!");
				e.printStackTrace();
			}
		}
    }
}